﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Loaddata.getsubsector" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Library.Enum;
using SIPO.Web.Support;
using Newtonsoft.Json.Linq;

namespace SIPO.Web.AjaxHandler.Loaddata
{
    public class getsubsector : SIPO.Web.BaseHandler
    {
        recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        string MethodName = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            if (context.Request.Params["method"] != null)
                MethodName = context.Request.Params["method"];

            if (MethodName == "getbyid")
            {
                int id = Convert.ToInt32(context.Request.Params["subsector_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetList();
                var result = subSectors.Where(x => x.SubSectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "aksikorporasi")
            {
                int id = Convert.ToInt32(context.Request.Params["sector_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetList();
                //var result = subSectors.Where(x => x.SectorID == id && x.SubSectorCode == "4");

                var result = subSectors.Where(x => x.SectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else if (MethodName == "getbysectorid_AND_subsubsectorids")
            {
                int id = Convert.ToInt32(context.Request.Params["sector_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetListBySectorID_AND_subsubsectorIDs(id, base.SubSubSectorIDs);
                var result = subSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "getbydebtorid")
            {
                int id = Convert.ToInt32(context.Request.Params["debtor_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetListByDebtorID_AND_subsubsectorIDs(id, base.SubSubSectorIDs);
                var result = subSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
            else if (MethodName == "registrasi")
            {
                int id = Convert.ToInt32(context.Request.Params["sector_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetList();
                var result = subSectors.Where(x => x.SectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else if (MethodName == "getall")
            {
                //int id = Convert.ToInt32(context.Request.Params["sector_id"]);
                List<recSubSector> subSectors = recSubSectorDal.GetList();
                var result = subSectors;

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else if(MethodName == "multipleSectorID")
            {
                string parameter = context.Request.Params["param"];
                parameter = parameter + " AND master.recSubSubSector.SubSubSectorID IN (" + base.SubSubSectorIDs + ")";
                recSubSectorRepo subSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                
                List<recSubSector> subSectors = subSectorDal.GetListMultipleSectorID(parameter);
                var result = subSectors;
                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }

            else
            {
                int id = Convert.ToInt32(context.Request.Params["sector_id"]);

                recDebtorSectorRepo debtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

                List<recSubSector> subSectors = null;
                if (base.DebtorID.HasValue)
                {
                    subSectors = new List<recSubSector>();
                    var sectors = debtorSectorDal.GetListByDebtor(base.DebtorID.Value);
                    sectors.ForEach(delegate(recDebtorSector domain)
                    {
                        if (!subSectors.Exists(x => x.SubSectorID == domain.recSubSector.SubSectorID))
                        {
                            recSubSector subObj = new recSubSector();
                            subObj.SectorID = domain.SectorID;
                            subObj.SubSectorID = domain.recSubSector.SubSectorID;
                            subObj.SubSectorCode = domain.recSubSector.SubSectorCode;
                            subObj.SubSectorName = domain.recSubSector.SubSectorName;
                            subSectors.Add(subObj);
                        }
                    });
                }
                else
                {
                    subSectors = recSubSectorDal.GetList();
                }

                var result = subSectors.Where(x => x.SectorID == id);

                context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
            }
        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}